<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Meanshift and Camshift</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../d3/dd5/tutorial_table_of_content_other.html">Other tutorials (ml, objdetect, photo, stitching, video)</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Meanshift and Camshift </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../d1/dc5/tutorial_background_subtraction.html">How to Use Background Subtraction Methods</a></p>
<p><b>Next Tutorial:</b> <a class="el" href="../../d4/dee/tutorial_optical_flow.html">Optical Flow</a></p>
<h2>Goal </h2>
<p>In this chapter,</p>
<ul>
<li>We will learn about the Meanshift and Camshift algorithms to track objects in videos.</li>
</ul>
<h2>Meanshift </h2>
<p>The intuition behind the meanshift is simple. Consider you have a set of points. (It can be a pixel distribution like histogram backprojection). You are given a small window (may be a circle) and you have to move that window to the area of maximum pixel density (or maximum number of points). It is illustrated in the simple image given below:</p>
<div class="image">
<img src="../../meanshift_basics.jpg" alt="meanshift_basics.jpg"/>
<div class="caption">
image</div></div>
<p> The initial window is shown in blue circle with the name "C1". Its original center is marked in blue rectangle, named "C1_o". But if you find the centroid of the points inside that window, you will get the point "C1_r" (marked in small blue circle) which is the real centroid of the window. Surely they don't match. So move your window such that the circle of the new window matches with the previous centroid. Again find the new centroid. Most probably, it won't match. So move it again, and continue the iterations such that the center of window and its centroid falls on the same location (or within a small desired error). So finally what you obtain is a window with maximum pixel distribution. It is marked with a green circle, named "C2". As you can see in the image, it has maximum number of points. The whole process is demonstrated on a static image below:</p>
<div class="image">
<img src="../../meanshift_face.gif" alt="meanshift_face.gif"/>
<div class="caption">
image</div></div>
<p> So we normally pass the histogram backprojected image and initial target location. When the object moves, obviously the movement is reflected in the histogram backprojected image. As a result, the meanshift algorithm moves our window to the new location with maximum density.</p>
<h3>Meanshift in OpenCV</h3>
<p>To use meanshift in OpenCV, first we need to setup the target, find its histogram so that we can backproject the target on each frame for calculation of meanshift. We also need to provide an initial location of window. For histogram, only Hue is considered here. Also, to avoid false values due to low light, low light values are discarded using <b><a class="el" href="../../d2/de8/group__core__array.html#ga48af0ab51e36436c5d04340e036ce981" title="Checks if array elements lie between the elements of two other arrays. ">cv.inRange()</a></b> function.</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/video/meanshift/meanshift.cpp">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d6/d87/imgcodecs_8hpp.html">opencv2/imgcodecs.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../dc/d3d/videoio_8hpp.html">opencv2/videoio.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d0/d1c/video_2include_2opencv2_2video_8hpp.html">opencv2/video.hpp</a>&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"><span class="keyword">using namespace </span>std;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)</div><div class="line">{</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">string</span> about =</div><div class="line">        <span class="stringliteral">&quot;This sample demonstrates the meanshift algorithm.\n&quot;</span></div><div class="line">        <span class="stringliteral">&quot;The example file can be downloaded from:\n&quot;</span></div><div class="line">        <span class="stringliteral">&quot;  https://www.bogotobogo.com/python/OpenCV_Python/images/mean_shift_tracking/slow_traffic_small.mp4&quot;</span>;</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">string</span> keys =</div><div class="line">        <span class="stringliteral">&quot;{ h help |      | print this help message }&quot;</span></div><div class="line">        <span class="stringliteral">&quot;{ @image |&lt;none&gt;| path to image file }&quot;</span>;</div><div class="line">    <a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html">CommandLineParser</a> parser(argc, argv, keys);</div><div class="line">    parser.about(about);</div><div class="line">    <span class="keywordflow">if</span> (parser.has(<span class="stringliteral">&quot;help&quot;</span>))</div><div class="line">    {</div><div class="line">        parser.printMessage();</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line">    <span class="keywordtype">string</span> filename = parser.get&lt;<span class="keywordtype">string</span>&gt;(<span class="stringliteral">&quot;@image&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span> (!parser.check())</div><div class="line">    {</div><div class="line">        parser.printErrors();</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line"></div><div class="line">    <a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html">VideoCapture</a> capture(filename);</div><div class="line">    <span class="keywordflow">if</span> (!capture.isOpened()){</div><div class="line">        <span class="comment">//error in opening the video input</span></div><div class="line">        cerr &lt;&lt; <span class="stringliteral">&quot;Unable to open file!&quot;</span> &lt;&lt; endl;</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> frame, roi, hsv_roi, <a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>;</div><div class="line">    <span class="comment">// take first frame of the video</span></div><div class="line">    capture &gt;&gt; frame;</div><div class="line"></div><div class="line">    <span class="comment">// setup initial location of window</span></div><div class="line">    <a class="code" href="../../d2/d44/classcv_1_1Rect__.html">Rect</a> track_window(300, 200, 100, 50); <span class="comment">// simply hardcoded the values</span></div><div class="line"></div><div class="line">    <span class="comment">// set up the ROI for tracking</span></div><div class="line">    roi = frame(track_window);</div><div class="line">    <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>(roi, hsv_roi, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0aa4a7f0ecf2e94150699e48c79139ee12">COLOR_BGR2HSV</a>);</div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga48af0ab51e36436c5d04340e036ce981">inRange</a>(hsv_roi, <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0, 60, 32), <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(180, 255, 255), mask);</div><div class="line"></div><div class="line">    <span class="keywordtype">float</span> range_[] = {0, 180};</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">float</span>* range[] = {range_};</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> roi_hist;</div><div class="line">    <span class="keywordtype">int</span> histSize[] = {180};</div><div class="line">    <span class="keywordtype">int</span> channels[] = {0};</div><div class="line">    <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga4b2b5fd75503ff9e6844cc4dcdaed35d">calcHist</a>(&amp;hsv_roi, 1, channels, mask, roi_hist, 1, histSize, range);</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1b6a396a456c8b6c6e4afd8591560d80">normalize</a>(roi_hist, roi_hist, 0, 255, <a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa9f0c1c342a18114d47b516a88e29822e">NORM_MINMAX</a>);</div><div class="line"></div><div class="line">    <span class="comment">// Setup the termination criteria, either 10 iteration or move by atleast 1 pt</span></div><div class="line">    <a class="code" href="../../d9/d5d/classcv_1_1TermCriteria.html">TermCriteria</a> term_crit(<a class="code" href="../../d9/d5d/classcv_1_1TermCriteria.html#a56fecdc291ccaba8aad27d67ccf72c57a857609e73e7028e638d2ea649f3b45d5">TermCriteria::EPS</a> | <a class="code" href="../../d9/d5d/classcv_1_1TermCriteria.html#a56fecdc291ccaba8aad27d67ccf72c57aeb9da694ea67b3ef7d524521b580867d">TermCriteria::COUNT</a>, 10, 1);</div><div class="line"></div><div class="line">    <span class="keywordflow">while</span>(<span class="keyword">true</span>){</div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> hsv, dst;</div><div class="line">        capture &gt;&gt; frame;</div><div class="line">        <span class="keywordflow">if</span> (frame.empty())</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>(frame, hsv, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0aa4a7f0ecf2e94150699e48c79139ee12">COLOR_BGR2HSV</a>);</div><div class="line">        <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga3a0af640716b456c3d14af8aee12e3ca">calcBackProject</a>(&amp;hsv, 1, channels, roi_hist, dst, range);</div><div class="line"></div><div class="line">        <span class="comment">// apply meanshift to get the new location</span></div><div class="line">        <a class="code" href="../../dc/d6b/group__video__track.html#ga432a563c94eaf179533ff1e83dbb65ea">meanShift</a>(dst, track_window, term_crit);</div><div class="line"></div><div class="line">        <span class="comment">// Draw it on image</span></div><div class="line">        <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga07d2f74cadcf8e305e810ce8eed13bc9">rectangle</a>(frame, track_window, 255, 2);</div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(<span class="stringliteral">&quot;img2&quot;</span>, frame);</div><div class="line"></div><div class="line">        <span class="keywordtype">int</span> keyboard = <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(30);</div><div class="line">        <span class="keywordflow">if</span> (keyboard == <span class="charliteral">&#39;q&#39;</span> || keyboard == 27)</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">    }</div><div class="line">}</div></div><!-- fragment -->  </div> </li>
</ul>
 <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://github.com/opencv/opencv/tree/master/samples/python/tutorial_code/video/meanshift/meanshift.py">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</div><div class="line"><span class="keyword">import</span> cv2 <span class="keyword">as</span> cv</div><div class="line"><span class="keyword">import</span> argparse</div><div class="line"></div><div class="line">parser = argparse.ArgumentParser(description=<span class="stringliteral">&#39;This sample demonstrates the meanshift algorithm. \</span></div><div class="line"><span class="stringliteral">                                              The example file can be downloaded from: \</span></div><div class="line"><span class="stringliteral">                                              https://www.bogotobogo.com/python/OpenCV_Python/images/mean_shift_tracking/slow_traffic_small.mp4&#39;</span>)</div><div class="line">parser.add_argument(<span class="stringliteral">&#39;image&#39;</span>, type=str, help=<span class="stringliteral">&#39;path to image file&#39;</span>)</div><div class="line">args = parser.parse_args()</div><div class="line"></div><div class="line">cap = <a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html">cv.VideoCapture</a>(args.image)</div><div class="line"></div><div class="line"><span class="comment"># take first frame of the video</span></div><div class="line">ret,frame = cap.read()</div><div class="line"></div><div class="line"><span class="comment"># setup initial location of window</span></div><div class="line">x, y, w, h = 300, 200, 100, 50 <span class="comment"># simply hardcoded the values</span></div><div class="line">track_window = (x, y, w, h)</div><div class="line"></div><div class="line"><span class="comment"># set up the ROI for tracking</span></div><div class="line">roi = frame[y:y+h, x:x+w]</div><div class="line">hsv_roi =  <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(roi, cv.COLOR_BGR2HSV)</div><div class="line">mask = <a class="code" href="../../d2/de8/group__core__array.html#ga48af0ab51e36436c5d04340e036ce981">cv.inRange</a>(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))</div><div class="line">roi_hist = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga6ca1876785483836f72a77ced8ea759a">cv.calcHist</a>([hsv_roi],[0],mask,[180],[0,180])</div><div class="line"><a class="code" href="../../d2/de8/group__core__array.html#ga7bcf47a1df78cf575162e0aed44960cb">cv.normalize</a>(roi_hist,roi_hist,0,255,cv.NORM_MINMAX)</div><div class="line"></div><div class="line"><span class="comment"># Setup the termination criteria, either 10 iteration or move by atleast 1 pt</span></div><div class="line">term_crit = ( cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1 )</div><div class="line"></div><div class="line">while(1):</div><div class="line">    ret, frame = cap.read()</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> ret == <span class="keyword">True</span>:</div><div class="line">        hsv = <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(frame, cv.COLOR_BGR2HSV)</div><div class="line">        dst = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#gab644bc90e7475cc047aa1b25dbcbd8df">cv.calcBackProject</a>([hsv],[0],roi_hist,[0,180],1)</div><div class="line"></div><div class="line">        <span class="comment"># apply meanshift to get the new location</span></div><div class="line">        ret, track_window = <a class="code" href="../../dc/d6b/group__video__track.html#ga432a563c94eaf179533ff1e83dbb65ea">cv.meanShift</a>(dst, track_window, term_crit)</div><div class="line"></div><div class="line">        <span class="comment"># Draw it on image</span></div><div class="line">        x,y,w,h = track_window</div><div class="line">        img2 = <a class="code" href="../../d6/d6e/group__imgproc__draw.html#gac865734d137287c0afb7682ff7b3db23">cv.rectangle</a>(frame, (x,y), (x+w,y+h), 255,2)</div><div class="line">        <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(<span class="stringliteral">&#39;img2&#39;</span>,img2)</div><div class="line"></div><div class="line">        k = <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv.waitKey</a>(30) &amp; 0xff</div><div class="line">        <span class="keywordflow">if</span> k == 27:</div><div class="line">            <span class="keywordflow">break</span></div><div class="line">    <span class="keywordflow">else</span>:</div><div class="line">        <span class="keywordflow">break</span></div></div><!-- fragment -->  </div> </li>
</ul>
 <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/video/meanshift/MeanshiftDemo.java">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"><span class="keyword">import</span> java.util.Arrays;</div><div class="line"><span class="keyword">import</span> org.opencv.core.*;</div><div class="line"><span class="keyword">import</span> org.opencv.highgui.HighGui;</div><div class="line"><span class="keyword">import</span> org.opencv.imgproc.Imgproc;</div><div class="line"><span class="keyword">import</span> org.opencv.video.Video;</div><div class="line"><span class="keyword">import</span> org.opencv.videoio.VideoCapture;</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">class </span>Meanshift {</div><div class="line">    <span class="keyword">public</span> <span class="keywordtype">void</span> run(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> filename = args[0];</div><div class="line">        VideoCapture capture = <span class="keyword">new</span> VideoCapture(filename);</div><div class="line">        <span class="keywordflow">if</span> (!capture.isOpened()) {</div><div class="line">            System.out.println(<span class="stringliteral">&quot;Unable to open file!&quot;</span>);</div><div class="line">            System.exit(-1);</div><div class="line">        }</div><div class="line">        Mat frame = <span class="keyword">new</span> Mat(), hsv_roi = <span class="keyword">new</span> Mat(), mask = <span class="keyword">new</span> Mat(), roi;</div><div class="line"></div><div class="line">        <span class="comment">// take the first frame of the video</span></div><div class="line">        capture.read(frame);</div><div class="line"></div><div class="line">        <span class="comment">//setup initial location of window</span></div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a> track_window = <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a>(300, 200, 100, 50);</div><div class="line"></div><div class="line">        <span class="comment">// setup initial location of window</span></div><div class="line">        roi = <span class="keyword">new</span> Mat(frame, track_window);</div><div class="line">        Imgproc.cvtColor(roi, hsv_roi, Imgproc.COLOR_BGR2HSV);</div><div class="line">        Core.inRange(hsv_roi, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0, 60, 32), <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(180, 255, 255), mask);</div><div class="line"></div><div class="line">        MatOfFloat range = <span class="keyword">new</span> MatOfFloat(0, 256);</div><div class="line">        Mat roi_hist = <span class="keyword">new</span> Mat();</div><div class="line">        MatOfInt histSize = <span class="keyword">new</span> MatOfInt(180);</div><div class="line">        MatOfInt channels = <span class="keyword">new</span> MatOfInt(0);</div><div class="line">        Imgproc.calcHist(Arrays.asList(hsv_roi), channels, <a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>, roi_hist, histSize, range);</div><div class="line">        Core.normalize(roi_hist, roi_hist, 0, 255, Core.NORM_MINMAX);</div><div class="line"></div><div class="line">        <span class="comment">// Setup the termination criteria, either 10 iteration or move by atleast 1 pt</span></div><div class="line">        TermCriteria term_crit = <span class="keyword">new</span> TermCriteria(TermCriteria.EPS | TermCriteria.COUNT, 10, 1);</div><div class="line"></div><div class="line">        <span class="keywordflow">while</span> (<span class="keyword">true</span>) {</div><div class="line">            Mat hsv = <span class="keyword">new</span> Mat() , dst = <span class="keyword">new</span> Mat();</div><div class="line">            capture.read(frame);</div><div class="line">            <span class="keywordflow">if</span> (frame.empty()) {</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            }</div><div class="line">            Imgproc.cvtColor(frame, hsv, Imgproc.COLOR_BGR2HSV);</div><div class="line">            Imgproc.calcBackProject(Arrays.asList(hsv), channels, roi_hist, dst, range, 1);</div><div class="line"></div><div class="line">            <span class="comment">// apply meanshift to get the new location</span></div><div class="line">            Video.meanShift(dst, track_window, term_crit);</div><div class="line"></div><div class="line">            <span class="comment">// Draw it on image</span></div><div class="line">            Imgproc.rectangle(frame, track_window, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(255, 0, 0), 2);</div><div class="line">            HighGui.imshow(<span class="stringliteral">&quot;img2&quot;</span>, frame);</div><div class="line"></div><div class="line">            <span class="keywordtype">int</span> keyboard = HighGui.waitKey(30);</div><div class="line">            <span class="keywordflow">if</span> (keyboard == <span class="charliteral">&#39;q&#39;</span> || keyboard == 27) {</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            }</div><div class="line">        }</div><div class="line">        System.exit(0);</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="keyword">class </span>MeanshiftDemo {</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keywordtype">void</span> main(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);</div><div class="line">        <span class="keyword">new</span> Meanshift().run(args);</div><div class="line">    }</div><div class="line">}</div></div><!-- fragment -->  </div> </li>
</ul>
<p>Three frames in a video I used is given below:</p>
<div class="image">
<img src="../../meanshift_result.jpg" alt="meanshift_result.jpg"/>
<div class="caption">
image</div></div>
 <h2>Camshift </h2>
<p>Did you closely watch the last result? There is a problem. Our window always has the same size whether the car is very far or very close to the camera. That is not good. We need to adapt the window size with size and rotation of the target. Once again, the solution came from "OpenCV Labs" and it is called CAMshift (Continuously Adaptive Meanshift) published by Gary Bradsky in his paper "Computer Vision Face Tracking for Use in a Perceptual User Interface" in 1998 <a class="el" href="../../d0/de3/citelist.html#CITEREF_Bradski98">[33]</a> .</p>
<p>It applies meanshift first. Once meanshift converges, it updates the size of the window as, \(s = 2 \times \sqrt{\frac{M_{00}}{256}}\). It also calculates the orientation of the best fitting ellipse to it. Again it applies the meanshift with new scaled search window and previous window location. The process continues until the required accuracy is met.</p>
<div class="image">
<img src="../../camshift_face.gif" alt="camshift_face.gif"/>
<div class="caption">
image</div></div>
 <h3>Camshift in OpenCV</h3>
<p>It is similar to meanshift, but returns a rotated rectangle (that is our result) and box parameters (used to be passed as search window in next iteration). See the code below:</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/video/meanshift/camshift.cpp">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d6/d87/imgcodecs_8hpp.html">opencv2/imgcodecs.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../dc/d3d/videoio_8hpp.html">opencv2/videoio.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d0/d1c/video_2include_2opencv2_2video_8hpp.html">opencv2/video.hpp</a>&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"><span class="keyword">using namespace </span>std;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)</div><div class="line">{</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">string</span> about =</div><div class="line">        <span class="stringliteral">&quot;This sample demonstrates the camshift algorithm.\n&quot;</span></div><div class="line">        <span class="stringliteral">&quot;The example file can be downloaded from:\n&quot;</span></div><div class="line">        <span class="stringliteral">&quot;  https://www.bogotobogo.com/python/OpenCV_Python/images/mean_shift_tracking/slow_traffic_small.mp4&quot;</span>;</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">string</span> keys =</div><div class="line">        <span class="stringliteral">&quot;{ h help |      | print this help message }&quot;</span></div><div class="line">        <span class="stringliteral">&quot;{ @image |&lt;none&gt;| path to image file }&quot;</span>;</div><div class="line">    <a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html">CommandLineParser</a> parser(argc, argv, keys);</div><div class="line">    parser.about(about);</div><div class="line">    <span class="keywordflow">if</span> (parser.has(<span class="stringliteral">&quot;help&quot;</span>))</div><div class="line">    {</div><div class="line">        parser.printMessage();</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line">    <span class="keywordtype">string</span> filename = parser.get&lt;<span class="keywordtype">string</span>&gt;(<span class="stringliteral">&quot;@image&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span> (!parser.check())</div><div class="line">    {</div><div class="line">        parser.printErrors();</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line"></div><div class="line">    <a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html">VideoCapture</a> capture(filename);</div><div class="line">    <span class="keywordflow">if</span> (!capture.isOpened()){</div><div class="line">        <span class="comment">//error in opening the video input</span></div><div class="line">        cerr &lt;&lt; <span class="stringliteral">&quot;Unable to open file!&quot;</span> &lt;&lt; endl;</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> frame, roi, hsv_roi, <a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>;</div><div class="line">    <span class="comment">// take first frame of the video</span></div><div class="line">    capture &gt;&gt; frame;</div><div class="line"></div><div class="line">    <span class="comment">// setup initial location of window</span></div><div class="line">    <a class="code" href="../../d2/d44/classcv_1_1Rect__.html">Rect</a> track_window(300, 200, 100, 50); <span class="comment">// simply hardcoded the values</span></div><div class="line"></div><div class="line">    <span class="comment">// set up the ROI for tracking</span></div><div class="line">    roi = frame(track_window);</div><div class="line">    <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>(roi, hsv_roi, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0aa4a7f0ecf2e94150699e48c79139ee12">COLOR_BGR2HSV</a>);</div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga48af0ab51e36436c5d04340e036ce981">inRange</a>(hsv_roi, <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0, 60, 32), <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(180, 255, 255), mask);</div><div class="line"></div><div class="line">    <span class="keywordtype">float</span> range_[] = {0, 180};</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">float</span>* range[] = {range_};</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> roi_hist;</div><div class="line">    <span class="keywordtype">int</span> histSize[] = {180};</div><div class="line">    <span class="keywordtype">int</span> channels[] = {0};</div><div class="line">    <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga4b2b5fd75503ff9e6844cc4dcdaed35d">calcHist</a>(&amp;hsv_roi, 1, channels, mask, roi_hist, 1, histSize, range);</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1b6a396a456c8b6c6e4afd8591560d80">normalize</a>(roi_hist, roi_hist, 0, 255, <a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa9f0c1c342a18114d47b516a88e29822e">NORM_MINMAX</a>);</div><div class="line"></div><div class="line">    <span class="comment">// Setup the termination criteria, either 10 iteration or move by atleast 1 pt</span></div><div class="line">    <a class="code" href="../../d9/d5d/classcv_1_1TermCriteria.html">TermCriteria</a> term_crit(<a class="code" href="../../d9/d5d/classcv_1_1TermCriteria.html#a56fecdc291ccaba8aad27d67ccf72c57a857609e73e7028e638d2ea649f3b45d5">TermCriteria::EPS</a> | <a class="code" href="../../d9/d5d/classcv_1_1TermCriteria.html#a56fecdc291ccaba8aad27d67ccf72c57aeb9da694ea67b3ef7d524521b580867d">TermCriteria::COUNT</a>, 10, 1);</div><div class="line"></div><div class="line">    <span class="keywordflow">while</span>(<span class="keyword">true</span>){</div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> hsv, dst;</div><div class="line">        capture &gt;&gt; frame;</div><div class="line">        <span class="keywordflow">if</span> (frame.empty())</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>(frame, hsv, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0aa4a7f0ecf2e94150699e48c79139ee12">COLOR_BGR2HSV</a>);</div><div class="line">        <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga3a0af640716b456c3d14af8aee12e3ca">calcBackProject</a>(&amp;hsv, 1, channels, roi_hist, dst, range);</div><div class="line"></div><div class="line">        <span class="comment">// apply camshift to get the new location</span></div><div class="line">        <a class="code" href="../../db/dd6/classcv_1_1RotatedRect.html">RotatedRect</a> rot_rect = <a class="code" href="../../dc/d6b/group__video__track.html#gaef2bd39c8356f423124f1fe7c44d54a1">CamShift</a>(dst, track_window, term_crit);</div><div class="line"></div><div class="line">        <span class="comment">// Draw it on image</span></div><div class="line">        <a class="code" href="../../db/d4e/classcv_1_1Point__.html">Point2f</a> points[4];</div><div class="line">        rot_rect.<a class="code" href="../../db/dd6/classcv_1_1RotatedRect.html#a69d648b086f26dbce0029facae9bfb2d">points</a>(points);</div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; 4; i++)</div><div class="line">            <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga7078a9fae8c7e7d13d24dac2520ae4a2">line</a>(frame, points[i], points[(i+1)%4], 255, 2);</div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(<span class="stringliteral">&quot;img2&quot;</span>, frame);</div><div class="line"></div><div class="line">        <span class="keywordtype">int</span> keyboard = <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(30);</div><div class="line">        <span class="keywordflow">if</span> (keyboard == <span class="charliteral">&#39;q&#39;</span> || keyboard == 27)</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">    }</div><div class="line">}</div></div><!-- fragment -->  </div> </li>
</ul>
 <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://github.com/opencv/opencv/tree/master/samples/python/tutorial_code/video/meanshift/camshift.py">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</div><div class="line"><span class="keyword">import</span> cv2 <span class="keyword">as</span> cv</div><div class="line"><span class="keyword">import</span> argparse</div><div class="line"></div><div class="line">parser = argparse.ArgumentParser(description=<span class="stringliteral">&#39;This sample demonstrates the camshift algorithm. \</span></div><div class="line"><span class="stringliteral">                                              The example file can be downloaded from: \</span></div><div class="line"><span class="stringliteral">                                              https://www.bogotobogo.com/python/OpenCV_Python/images/mean_shift_tracking/slow_traffic_small.mp4&#39;</span>)</div><div class="line">parser.add_argument(<span class="stringliteral">&#39;image&#39;</span>, type=str, help=<span class="stringliteral">&#39;path to image file&#39;</span>)</div><div class="line">args = parser.parse_args()</div><div class="line"></div><div class="line">cap = <a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html">cv.VideoCapture</a>(args.image)</div><div class="line"></div><div class="line"><span class="comment"># take first frame of the video</span></div><div class="line">ret,frame = cap.read()</div><div class="line"></div><div class="line"><span class="comment"># setup initial location of window</span></div><div class="line">x, y, w, h = 300, 200, 100, 50 <span class="comment"># simply hardcoded the values</span></div><div class="line">track_window = (x, y, w, h)</div><div class="line"></div><div class="line"><span class="comment"># set up the ROI for tracking</span></div><div class="line">roi = frame[y:y+h, x:x+w]</div><div class="line">hsv_roi =  <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(roi, cv.COLOR_BGR2HSV)</div><div class="line">mask = <a class="code" href="../../d2/de8/group__core__array.html#ga48af0ab51e36436c5d04340e036ce981">cv.inRange</a>(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))</div><div class="line">roi_hist = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga6ca1876785483836f72a77ced8ea759a">cv.calcHist</a>([hsv_roi],[0],mask,[180],[0,180])</div><div class="line"><a class="code" href="../../d2/de8/group__core__array.html#ga7bcf47a1df78cf575162e0aed44960cb">cv.normalize</a>(roi_hist,roi_hist,0,255,cv.NORM_MINMAX)</div><div class="line"></div><div class="line"><span class="comment"># Setup the termination criteria, either 10 iteration or move by atleast 1 pt</span></div><div class="line">term_crit = ( cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1 )</div><div class="line"></div><div class="line">while(1):</div><div class="line">    ret, frame = cap.read()</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> ret == <span class="keyword">True</span>:</div><div class="line">        hsv = <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(frame, cv.COLOR_BGR2HSV)</div><div class="line">        dst = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#gab644bc90e7475cc047aa1b25dbcbd8df">cv.calcBackProject</a>([hsv],[0],roi_hist,[0,180],1)</div><div class="line"></div><div class="line">        <span class="comment"># apply camshift to get the new location</span></div><div class="line">        ret, track_window = <a class="code" href="../../dc/d6b/group__video__track.html#gaef2bd39c8356f423124f1fe7c44d54a1">cv.CamShift</a>(dst, track_window, term_crit)</div><div class="line"></div><div class="line">        <span class="comment"># Draw it on image</span></div><div class="line">        pts = <a class="code" href="../../d3/dc0/group__imgproc__shape.html#gaf78d467e024b4d7936cf9397185d2f5c">cv.boxPoints</a>(ret)</div><div class="line">        pts = np.int0(pts)</div><div class="line">        img2 = <a class="code" href="../../d6/d6e/group__imgproc__draw.html#gaa3c25f9fb764b6bef791bf034f6e26f5">cv.polylines</a>(frame,[pts],<span class="keyword">True</span>, 255,2)</div><div class="line">        <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(<span class="stringliteral">&#39;img2&#39;</span>,img2)</div><div class="line"></div><div class="line">        k = <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv.waitKey</a>(30) &amp; 0xff</div><div class="line">        <span class="keywordflow">if</span> k == 27:</div><div class="line">            <span class="keywordflow">break</span></div><div class="line">    <span class="keywordflow">else</span>:</div><div class="line">        <span class="keywordflow">break</span></div></div><!-- fragment -->  </div> </li>
</ul>
 <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/video/meanshift/CamshiftDemo.java">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"><span class="keyword">import</span> java.util.Arrays;</div><div class="line"><span class="keyword">import</span> org.opencv.core.*;</div><div class="line"><span class="keyword">import</span> org.opencv.highgui.HighGui;</div><div class="line"><span class="keyword">import</span> org.opencv.imgproc.Imgproc;</div><div class="line"><span class="keyword">import</span> org.opencv.video.Video;</div><div class="line"><span class="keyword">import</span> org.opencv.videoio.VideoCapture;</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">class </span>Camshift {</div><div class="line">    <span class="keyword">public</span> <span class="keywordtype">void</span> run(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> filename = args[0];</div><div class="line">        VideoCapture capture = <span class="keyword">new</span> VideoCapture(filename);</div><div class="line">        <span class="keywordflow">if</span> (!capture.isOpened()) {</div><div class="line">            System.out.println(<span class="stringliteral">&quot;Unable to open file!&quot;</span>);</div><div class="line">            System.exit(-1);</div><div class="line">        }</div><div class="line"></div><div class="line">        Mat frame = <span class="keyword">new</span> Mat(), hsv_roi = <span class="keyword">new</span> Mat(), mask = <span class="keyword">new</span> Mat(), roi;</div><div class="line"></div><div class="line">        <span class="comment">// take the first frame of the video</span></div><div class="line">        capture.read(frame);</div><div class="line"></div><div class="line">        <span class="comment">//setup initial location of window</span></div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a> track_window = <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a>(300, 200, 100, 50);</div><div class="line"></div><div class="line">        <span class="comment">// set up the ROI for tracking</span></div><div class="line">        roi = <span class="keyword">new</span> Mat(frame, track_window);</div><div class="line">        Imgproc.cvtColor(roi, hsv_roi, Imgproc.COLOR_BGR2HSV);</div><div class="line">        Core.inRange(hsv_roi, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0, 60, 32), <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(180, 255, 255), mask);</div><div class="line"></div><div class="line">        MatOfFloat range = <span class="keyword">new</span> MatOfFloat(0, 256);</div><div class="line">        Mat roi_hist = <span class="keyword">new</span> Mat();</div><div class="line">        MatOfInt histSize = <span class="keyword">new</span> MatOfInt(180);</div><div class="line">        MatOfInt channels = <span class="keyword">new</span> MatOfInt(0);</div><div class="line">        Imgproc.calcHist(Arrays.asList(hsv_roi), channels, <a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>, roi_hist, histSize, range);</div><div class="line">        Core.normalize(roi_hist, roi_hist, 0, 255, Core.NORM_MINMAX);</div><div class="line"></div><div class="line">        <span class="comment">// Setup the termination criteria, either 10 iteration or move by atleast 1 pt</span></div><div class="line">        TermCriteria term_crit = <span class="keyword">new</span> TermCriteria(TermCriteria.EPS | TermCriteria.COUNT, 10, 1);</div><div class="line"></div><div class="line">        <span class="keywordflow">while</span> (<span class="keyword">true</span>) {</div><div class="line">            Mat hsv = <span class="keyword">new</span> Mat() , dst = <span class="keyword">new</span> Mat();</div><div class="line">            capture.read(frame);</div><div class="line">            <span class="keywordflow">if</span> (frame.empty()) {</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            }</div><div class="line">            Imgproc.cvtColor(frame, hsv, Imgproc.COLOR_BGR2HSV);</div><div class="line">            Imgproc.calcBackProject(Arrays.asList(hsv), channels, roi_hist, dst, range, 1);</div><div class="line"></div><div class="line">            <span class="comment">// apply camshift to get the new location</span></div><div class="line">            RotatedRect rot_rect = Video.CamShift(dst, track_window, term_crit);</div><div class="line"></div><div class="line">            <span class="comment">// Draw it on image</span></div><div class="line">            <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>[] points = <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>[4];</div><div class="line">            rot_rect.points(points);</div><div class="line">            <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; 4 ;i++) {</div><div class="line">                Imgproc.line(frame, points[i], points[(i+1)%4], <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(255, 0, 0),2);</div><div class="line">            }</div><div class="line"></div><div class="line">            HighGui.imshow(<span class="stringliteral">&quot;img2&quot;</span>, frame);</div><div class="line">            <span class="keywordtype">int</span> keyboard = HighGui.waitKey(30);</div><div class="line">            <span class="keywordflow">if</span> (keyboard == <span class="charliteral">&#39;q&#39;</span>|| keyboard == 27) {</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            }</div><div class="line">        }</div><div class="line">        System.exit(0);</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="keyword">class </span>CamshiftDemo {</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keywordtype">void</span> main(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);</div><div class="line">        <span class="keyword">new</span> Camshift().run(args);</div><div class="line">    }</div><div class="line">}</div></div><!-- fragment -->  </div> </li>
</ul>
<p>Three frames of the result is shown below:</p>
<div class="image">
<img src="../../camshift_result.jpg" alt="camshift_result.jpg"/>
<div class="caption">
image</div></div>
 <h2>Additional Resources </h2>
<ol type="1">
<li>French Wikipedia page on <a href="http://fr.wikipedia.org/wiki/Camshift">Camshift</a>. (The two animations are taken from there)</li>
<li>Bradski, G.R., "Real time face and object tracking as a component of a perceptual user
    interface," Applications of Computer Vision, 1998. WACV '98. Proceedings., Fourth IEEE Workshop on , vol., no., pp.214,219, 19-21 Oct 1998</li>
</ol>
<h2>Exercises </h2>
<ol type="1">
<li>OpenCV comes with a Python <a href="https://github.com/opencv/opencv/blob/master/samples/python/camshift.py">sample</a> for an interactive demo of camshift. Use it, hack it, understand it. </li>
</ol>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:37 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
